home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / DateTimeDemo.java < prev    next >
Text File  |  1998-09-15  |  28KB  |  958 lines

  1. /*
  2.  * @(#)DateTimeDemo.java     1.1 98/06/16
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30.  
  31. import java.applet.Applet;
  32. import java.awt.event.*;
  33. import java.awt.*;
  34. import java.lang.*;
  35. import java.util.*;
  36. import java.net.*;
  37. import java.io.*;
  38. import java.text.*;
  39.  
  40.  
  41. /**
  42.  * DateTimeDemo demonstrates how Date/Time formatter works. 
  43.  */
  44. public class DateTimeDemo extends DemoApplet
  45. {
  46.     /**
  47.      * The main function which defines the behavior of the DateTimeDemo
  48.      * applet when an applet is started.
  49.      */
  50.     public static void main(String argv[]) {
  51.         DemoApplet.showDemo(new DateTimeFrame(null));
  52.     }
  53.  
  54.     /**
  55.      * This creates a DateTimeFrame for the demo applet.
  56.      */
  57.     public Frame createDemoFrame(DemoApplet applet) {
  58.         return new DateTimeFrame(applet);
  59.     }
  60. }
  61.  
  62. /**
  63.  * A Frame is a top-level window with a title. The default layout for a frame
  64.  * is BorderLayout.  The DateTimeFrame class defines the window layout of
  65.  * DateTimeDemo.
  66.  */
  67. class DateTimeFrame extends Frame implements WindowListener, ActionListener, ItemListener, KeyListener
  68. {
  69.     private static final String creditString
  70.         = "";
  71.     private static final String copyrightString
  72.         = "";
  73.     private static final String copyrightString2
  74.         = "";
  75.  
  76.     private static final int FIELD_COLUMNS = 45;
  77.  
  78.     private static final boolean DEBUG = false;
  79.  
  80.     private static final int millisPerHour = 60 * 60 * 1000;
  81.     private boolean isLocalized = false;
  82.     private boolean validationMode = false;
  83.     private Locale curLocale = Locale.US;
  84.  
  85.     private SimpleDateFormat format;
  86.     private Locale[] locales;
  87.     private DemoApplet applet;
  88.  
  89.     // Mapping tables for displaying Rep. Cities for given timezones.
  90.     private static final int kAdjCityIndex[]
  91.     // many-to-1 mapping:
  92.     // locale index --> rep city index
  93.     = { 1,
  94.         3,
  95.         2,
  96.         4,
  97.         0,
  98.         5,  // eg, Locale Index: 5 --> Rep. City index: 5
  99.     0,
  100.     0,
  101.         6};
  102.  
  103.     private static final int kZoneOffsets[]
  104.     // 1-to-1 maping:
  105.     // kZoneOffsets returns the zone offset for a given rep. city index.
  106.     = { 1*millisPerHour,
  107.        -8*millisPerHour,
  108.                       0,
  109.        -5*millisPerHour,
  110.        -7*millisPerHour,
  111.        -6*millisPerHour,
  112.         9*millisPerHour};
  113.  
  114.     private static final String kZoneIDs[]
  115.     // 1-1 maping:
  116.     // kZoneIDs returns the zone ID string for a given rep. city index.
  117.     = {"ECT",
  118.        "PST",
  119.        "GMT",
  120.        "EST",
  121.        "MST",
  122.        "CST",
  123.        "JST"};
  124.  
  125.  
  126.     /**
  127.      * Constructs a new DateTimeFrame that is initially invisible.
  128.      */
  129.     public DateTimeFrame(DemoApplet applet)
  130.     {
  131.         super("Date/Time Formatting Demo");
  132.         this.applet = applet;
  133.     addWindowListener(this);
  134.         init();
  135.         start();
  136.     }
  137.  
  138.  
  139.     /**
  140.      * Initializes the applet. You never need to call this directly, it
  141.      * is called automatically by the system once the applet is created.
  142.      */
  143.     public void init()
  144.     {
  145.         // Get G7 locales only for demo purpose. To get all the locales
  146.         // supported, switch to calling TimeFormat.getAvailableLocales().
  147.         // commented.  However, the mapping tables such as kAdjCityIndex
  148.         // must be expended as well.
  149.         locales = Utility.getG7Locales();
  150. //        locales = TimeFormat.getAvailableLocales();
  151.  
  152.         buildGUI();
  153.  
  154.         // Stick the names of the locales into the locale popup menu
  155.         Locale displayLocale = Locale.getDefault();
  156.         for (int i = 0; i < locales.length; i++) {
  157.             if (locales[i].getCountry().length() > 0) {
  158.                 localeMenu.addItem( locales[i].getDisplayName() );
  159.                 if (locales[i].equals(Locale.getDefault())) {
  160.                     localeMenu.select(i);
  161.                 }
  162.             }
  163.         }
  164.  
  165.         // For starters, use the default format for the selected locale
  166.         // in the menu
  167.         setFormatFromLocale(true);
  168.         formatText();
  169.     }
  170.  
  171.     //------------------------------------------------------------
  172.     // package private
  173.     //------------------------------------------------------------
  174.     void addWithFont(Container container, Component foo, Font font) {
  175.         if (font != null)
  176.             foo.setFont(font);
  177.         container.add(foo);
  178.     }
  179.  
  180.     /**
  181.      * Called to start the applet. You never need to call this method
  182.      * directly, it is called when the applet's document is visited.
  183.      */
  184.     public void start()
  185.     {
  186.         // do nothing
  187.     }
  188.  
  189.     /**
  190.      * This function is called when it is necessary to display a new
  191.      * format pattern. This happens when the state of the "Localized Pattern"
  192.      * CheckBox is changed.
  193.      */
  194.     public void handleNewFormat()
  195.     {
  196.         Utility.setText(patternText, format.toPattern() );
  197.     }
  198.  
  199.     /**
  200.      * This function is called when users change the Calendar (time fields)
  201.      * validation mode. When the state of the "Validation Mode" CheckBox
  202.      * is changed, the time string in the "New Date" text field will be
  203.      * re-parsed, and the parsing result will be displayed in the "1.0 Date"
  204.      * text field.
  205.      */
  206.     public void validationModeChanged()
  207.     {
  208.         format.setLenient(validationMode);
  209.  
  210.         parseText();
  211.     }
  212.  
  213.     //{{DECLARE_CONTROLS
  214.     Panel localePanel;
  215.     Panel formatPanel;
  216.     CheckboxGroup group1;
  217.     CheckboxGroup group2;
  218.     Label label1;
  219.     Label label2;
  220.     Label label3;
  221.     Label demo;
  222.     Label code;
  223.     Choice localeMenu;
  224.     Choice dateStyleMenu;
  225.     Choice timeStyleMenu;
  226.     Choice dateMenu;
  227.     Choice cityMenu;
  228.     Label dateLabel;
  229.     Label cityLabel;
  230.     TextField millisText;
  231.     Label millisLabel;
  232.     Button up;
  233.     Button down;
  234.     Label localeLabel;
  235.     Label dateStyleLabel;
  236.     Label timeStyleLabel;
  237.     TextField inputText;
  238.     TextField outputText;
  239.     Label formatLabel;
  240.     Label parseLabel;
  241.     //Button rightButton;
  242.     //Button leftButton;
  243.     TextField patternText;
  244.     Label label4;
  245.     Checkbox getDateInstance;
  246.     Checkbox getTimeInstance;
  247.     Checkbox getDateTimeInstance;
  248.     Checkbox getRoll;
  249.     Checkbox getAdd;
  250.     Checkbox getLocalized;
  251.     Checkbox getValidationMode;
  252.  
  253.     //}}
  254.  
  255.     Color color = Color.white;
  256.     public void buildGUI()
  257.     {
  258.         //{{INIT_CONTROLS
  259.  
  260.         setBackground(color);
  261.         setLayout(new FlowLayout()); // shouldn't be necessary, but it is.
  262.  
  263. // TITLE
  264.         Panel titlePanel = new Panel();
  265.  
  266.  
  267.         label1=new Label("Date/Time Formatting Demo", Label.CENTER);
  268.         label1.setFont(Utility.titleFont);
  269.  
  270.         titlePanel.add(label1);
  271.  
  272. // CREDITS
  273.  
  274.         Panel creditPanel = new Panel();
  275.  
  276.         demo=new Label(creditString, Label.CENTER);
  277.         demo.setFont(Utility.creditFont);
  278.         creditPanel.add(demo);
  279.  
  280.         titlePanel.add(creditPanel);
  281.  
  282.         Utility.fixGrid(titlePanel,1);
  283.          add(titlePanel);
  284.  
  285.  
  286. // IO Panel
  287.         Panel topPanel = new Panel();
  288.         topPanel.setLayout(new FlowLayout());
  289.  
  290.         label3=new Label("New Date", Label.RIGHT);
  291.         label3.setFont(Utility.labelFont);
  292.         topPanel.add(label3);
  293.  
  294.         outputText=new TextField(FIELD_COLUMNS);
  295.     outputText.addKeyListener(this);
  296.         outputText.setFont(Utility.editFont);
  297.         topPanel.add(outputText);
  298.  
  299.  
  300.         label2=new Label("1.0 Date", Label.RIGHT);
  301.         label2.setFont(Utility.labelFont);
  302.         topPanel.add(label2);
  303.  
  304.     // intentional use of deprecated method
  305.         inputText=new TextField(new Date().toGMTString(),FIELD_COLUMNS);
  306.     inputText.addKeyListener(this);
  307.         inputText.setFont(Utility.editFont);
  308.         topPanel.add(inputText);
  309.  
  310.  
  311.         millisLabel=new Label("Millis", Label.RIGHT);
  312.         millisLabel.setFont(Utility.labelFont);
  313.         topPanel.add(millisLabel);
  314.  
  315.         millisText=new TextField(FIELD_COLUMNS);
  316.     millisText.addKeyListener(this);
  317.         millisText.setFont(Utility.editFont);
  318.         topPanel.add(millisText);
  319.  
  320.         label4=new Label("Pattern", Label.RIGHT);
  321.         label4.setFont(Utility.labelFont);
  322.         topPanel.add(label4);
  323.  
  324.         patternText=new TextField(FIELD_COLUMNS);
  325.     patternText.addKeyListener(this);
  326.         patternText.setFont(Utility.editFont);
  327.         topPanel.add(patternText);
  328.  
  329.         topPanel.add(new Label(" "));
  330.  
  331.         getLocalized=new Checkbox("Localized Pattern");
  332.     getLocalized.addItemListener(this);
  333.         getLocalized.setFont(Utility.labelFont);
  334.  
  335.         getValidationMode=new Checkbox("Validation Mode");
  336.     getValidationMode.addItemListener(this);
  337.         getValidationMode.setFont(Utility.labelFont);
  338.  
  339.         Panel checkBoxesPanel = new Panel();
  340.         checkBoxesPanel.setLayout(new GridLayout(1,2,40,0));
  341.         checkBoxesPanel.add(getLocalized);
  342.         checkBoxesPanel.add(getValidationMode);
  343.  
  344.         topPanel.add(checkBoxesPanel);
  345.  
  346.         Utility.fixGrid(topPanel,2);
  347.          add(topPanel);
  348.  
  349. // DATE
  350.  
  351.         Panel datePanel=new Panel();
  352.         datePanel.setLayout(new FlowLayout());
  353.  
  354.         group2= new CheckboxGroup();
  355.  
  356.         getRoll=new Checkbox("Roll",group2, true);
  357.         getAdd=new Checkbox("Add",group2, false);
  358.     getRoll.addItemListener(this);
  359.     getAdd.addItemListener(this);
  360.  
  361.         dateLabel=new Label("Date Fields");
  362.         dateLabel.setFont(Utility.labelFont);
  363.  
  364.         Panel upDown = new Panel();
  365.         upDown.setLayout(new GridLayout(2,1));
  366.  
  367.         // *** If the images are not found, we use the label.
  368.         up = new Button("^");
  369.         down = new Button("v");
  370.     up.addActionListener(this);
  371.     down.addActionListener(this);
  372.         up.setBackground(Color.lightGray);
  373.         down.setBackground(Color.lightGray);
  374.         upDown.add(up);
  375.         upDown.add(down);
  376.  
  377.  
  378.         Panel rollAddBoxes = new Panel();
  379.         rollAddBoxes.setLayout(new GridLayout(2,1));
  380.  
  381.         rollAddBoxes.add(getRoll);
  382.         rollAddBoxes.add(getAdd);
  383.  
  384.         Panel rollAddPanel = new Panel();
  385.         rollAddPanel.setLayout(new FlowLayout());
  386.         rollAddPanel.add(rollAddBoxes);
  387.         rollAddPanel.add(upDown);
  388.  
  389.         dateMenu= new Choice();
  390.         dateMenu.addItem( "Year");
  391.         dateMenu.addItem( "Month");
  392.         dateMenu.addItem( "Day of Month");
  393.         dateMenu.addItem( "Hour of Day");
  394.         dateMenu.addItem( "Minute");
  395.         dateMenu.addItem( "Second");
  396.         dateMenu.addItem( "Millisecond");
  397.  
  398.         Panel dateLM = new Panel();
  399.         dateLM.setLayout(new GridLayout(2,1));
  400.         dateLM.add(dateLabel);
  401.         dateLM.add(dateMenu);
  402.  
  403.         datePanel.add(dateLM);
  404.  
  405. // CITIES
  406.  
  407.         Panel citiesPanel=new Panel();
  408.         citiesPanel.setLayout(new FlowLayout());
  409.         Panel cityPanel=new Panel();
  410.         cityPanel.setLayout(new GridLayout(2,1));
  411.         cityMenu= new Choice();
  412.     cityMenu.addItemListener(this);
  413.         cityMenu.addItem( "Paris");
  414.         cityMenu.addItem( "Copenhagen");
  415.         cityMenu.addItem( "London");
  416.         cityMenu.addItem( "Washington");
  417.         cityMenu.addItem( "Toronto");
  418.         cityMenu.addItem( "Montreal");
  419.         cityMenu.addItem( "Tokyo");
  420.  
  421.         cityLabel=new Label("City");
  422.         cityLabel.setFont(Utility.labelFont);
  423.  
  424.         cityPanel.add(cityLabel);
  425.         cityPanel.add(cityMenu);
  426.         citiesPanel.add(cityPanel);
  427.  
  428.         Panel cityDatePanel = new Panel();
  429.         cityDatePanel.add(citiesPanel);
  430.         cityDatePanel.add(datePanel);
  431.         cityDatePanel.add(rollAddPanel);    // choices
  432.         Utility.fixGrid(cityDatePanel,1);
  433.          add(cityDatePanel);
  434.  
  435. // BORDER
  436.         // true means raised, false = depressed
  437.         BorderPanel borderPanel = new BorderPanel(true);
  438.         borderPanel.setBackground(Color.lightGray);
  439.         borderPanel.setLayout(null);
  440.         borderPanel.setSize(8,150);
  441.          add(borderPanel);
  442.  
  443. // LOCALE
  444.  
  445.         // sets up localePanel
  446.         localePanel=new Panel();
  447.         localePanel.setLayout(new GridLayout(2,1));
  448.  
  449.         localeLabel=new Label("Locale");
  450.         localeLabel.setFont(Utility.labelFont);
  451.         localeMenu= new Choice();
  452.     localeMenu.addItemListener(this);
  453.  
  454.         localePanel.add("loc",localeLabel);
  455.         localePanel.add(localeMenu);
  456.  
  457.         // sets up formatPanel
  458.         formatPanel=new Panel();
  459.  
  460.         group1= new CheckboxGroup();
  461.         getDateInstance=new Checkbox("Date Format",group1, false);
  462.         getTimeInstance=new Checkbox("Time Format",group1, false);
  463.         getDateTimeInstance=new Checkbox("Date and Time Format",group1, true);
  464.  
  465.     getDateInstance.addItemListener(this);
  466.     getTimeInstance.addItemListener(this);
  467.     getDateTimeInstance.addItemListener(this);
  468.  
  469.         Panel formatButtons = new Panel();
  470.         formatButtons.setLayout(new GridLayout(3,1));
  471.  
  472.         formatButtons.add(getDateInstance);
  473.         formatButtons.add(getTimeInstance);
  474.         formatButtons.add(getDateTimeInstance);
  475.  
  476.         Panel dateStylePanel=new Panel();
  477.         dateStylePanel.setLayout(new GridLayout(2,1));
  478.         dateStyleLabel=new Label("Date Style");
  479.         dateStyleLabel.setFont(Utility.labelFont);
  480.         dateStyleMenu= new Choice();
  481.     dateStyleMenu.addItemListener(this);
  482.         dateStyleMenu.addItem("Full");
  483.         dateStyleMenu.addItem("Long");
  484.         dateStyleMenu.addItem("Default");
  485.         dateStyleMenu.addItem("Short");
  486.         dateStylePanel.add("loc",dateStyleLabel);
  487.         dateStylePanel.add(dateStyleMenu);
  488.  
  489.         Panel timeStylePanel=new Panel();
  490.         timeStylePanel.setLayout(new GridLayout(2,1));
  491.         timeStyleLabel=new Label("Time Style");
  492.         timeStyleLabel.setFont(Utility.labelFont);
  493.         timeStyleMenu= new Choice();
  494.     timeStyleMenu.addItemListener(this);
  495.         timeStyleMenu.addItem("Full");
  496.         timeStyleMenu.addItem("Long");
  497.         timeStyleMenu.addItem("Default");
  498.         timeStyleMenu.addItem("Short");
  499.         timeStylePanel.add("loc",timeStyleLabel);
  500.         timeStylePanel.add(timeStyleMenu);
  501.  
  502.         Panel dtStylePanel = new Panel();
  503.         dtStylePanel.setLayout(new GridLayout(1,2,20,0));
  504.         dtStylePanel.add(dateStylePanel);
  505.         dtStylePanel.add(timeStylePanel);
  506.  
  507.         formatPanel.add(formatButtons);
  508.         formatPanel.add(dtStylePanel);
  509.         Utility.fixGrid(formatPanel,1);
  510.  
  511.         Panel localesFormatPanel = new Panel();
  512.  
  513.         localesFormatPanel.add(localePanel);
  514.         localesFormatPanel.add(formatPanel);
  515.         Utility.fixGrid(localesFormatPanel,1);
  516.          add(localesFormatPanel);
  517.  
  518.         Panel copyrightPanel = new Panel();
  519.         addWithFont (copyrightPanel,new Label(copyrightString, Label.LEFT),
  520.             Utility.creditFont);
  521.         addWithFont (copyrightPanel,new Label(copyrightString2, Label.LEFT),
  522.             Utility.creditFont);
  523.         Utility.fixGrid(copyrightPanel,1);
  524.          add(copyrightPanel);
  525.  
  526.         //}}
  527.  
  528.     }
  529.  
  530.  
  531.   /* ActionListener method */
  532.   public void actionPerformed(ActionEvent e) {
  533.     if (e.getSource() == up){
  534.       dateFieldChanged(true);
  535.     } else if (e.getSource() == down) {
  536.       dateFieldChanged(false);
  537.     }
  538.   }
  539.  
  540.   /* ItemListener method */
  541.   public void itemStateChanged(ItemEvent e) {
  542.     if (e.getSource() == localeMenu) {
  543.       selectedLocaleMenu();
  544.     } else if (e.getSource() == dateStyleMenu) {
  545.       selectedDateStyleMenu();
  546.     } else if (e.getSource() == timeStyleMenu) {
  547.       selectedTimeStyleMenu();
  548.     } else if (e.getSource() == cityMenu) {
  549.       cityChanged();
  550.     } else if (e.getSource() == getRoll) {
  551.       clickedGetRoll();
  552.     } else if (e.getSource() == getAdd) {
  553.       clickedGetAdd();
  554.     } else if (e.getSource() == getLocalized) {
  555.       isLocalized = getLocalized.getState();
  556.       handleNewFormat();
  557.     } else if (e.getSource() == getValidationMode) {
  558.       validationMode = getValidationMode.getState();
  559.       validationModeChanged();      
  560.     } else if (e.getSource() == getDateInstance) {
  561.       clickedGetDateFormat();
  562.     } else if (e.getSource() == getTimeInstance) {
  563.       clickedGetTimeFormat();
  564.     } else if (e.getSource() == getDateTimeInstance) {
  565.       clickedGetDateTimeFormat();
  566.     }
  567.  
  568.   }
  569.   
  570.   /* KeyListener methods */
  571.   public void keyPressed(KeyEvent e) {
  572.   }
  573.  
  574.   public void keyReleased(KeyEvent e) {
  575.     if (e.getSource() == patternText) {
  576.       e.consume();
  577.       patternTextChanged();
  578.     } else if (e.getSource() == inputText) {
  579.       e.consume();
  580.       formatText();
  581.     } else if (e.getSource() == outputText) {
  582.       e.consume();
  583.       parseText();
  584.     } else if (e.getSource() == millisText) {
  585.       e.consume();
  586.       millisChanged();
  587.     }
  588.   }
  589.  
  590.   public void keyTyped(KeyEvent e) {
  591.   }
  592.   
  593.   /* Window Listener methods */
  594.   public void windowClosed(WindowEvent e) {
  595.   }
  596.  
  597.   public void windowDeiconified(WindowEvent e) {
  598.   }
  599.  
  600.   public void windowIconified(WindowEvent e) {
  601.   }
  602.  
  603.   public void windowActivated(WindowEvent e) {
  604.   }
  605.  
  606.   public void windowDeactivated(WindowEvent e) {
  607.   }
  608.  
  609.   public void windowOpened(WindowEvent e) {
  610.   }
  611.  
  612.   public void windowClosing(WindowEvent e) {
  613.     setVisible(false);
  614.     dispose();
  615.  
  616.     if (applet != null) {
  617.       applet.demoClosed();
  618.     } else System.exit(0);
  619.  
  620.   }
  621.  
  622.  
  623.     /**
  624.      * This function is called when users select a new time and/or date
  625.      * format pattern, or a new locale.
  626.      */
  627.     public void setFormatFromLocale(boolean localChanged) {
  628.         int localeIndex = localeMenu.getSelectedIndex();
  629.         int dateStyleIndex = dateStyleMenu.getSelectedIndex()
  630.                              + DateFormat.FULL;
  631.         int timeStyleIndex = timeStyleMenu.getSelectedIndex()
  632.                              + DateFormat.FULL;
  633.         if (localChanged)
  634.             // Find the locale corresponding to the selected menu item
  635.             curLocale = locales[localeIndex];
  636.  
  637.         if (getDateInstance.getState()) {
  638.             format =
  639.             (SimpleDateFormat) DateFormat.getDateInstance(dateStyleIndex,
  640.                                                         curLocale);
  641.         } else if (getTimeInstance.getState()) {
  642.             format =
  643.             (SimpleDateFormat) DateFormat.getTimeInstance(timeStyleIndex,
  644.                                                         curLocale);
  645.         } else {
  646.             format =
  647.             (SimpleDateFormat) DateFormat.getDateTimeInstance(dateStyleIndex,
  648.                                                             timeStyleIndex,
  649.                                                             curLocale);
  650.         }
  651.  
  652.         patternText.setText( format.toPattern() );
  653.         if (!localChanged)
  654.         {
  655.             // locale not changed, only pattern format is changed.
  656.             setMillisText();
  657.             millisFormat();
  658.         }
  659.         else // change to selecting a rep. city based on new locale selected
  660.         {
  661.             cityMenu.select(kAdjCityIndex[localeIndex]);
  662.             cityChanged();
  663.         }
  664.     }
  665.  
  666.     /**
  667.      * This function is called when users change the pattern text.
  668.      */
  669.     public void setFormatFromPattern() {
  670.         String timePattern = patternText.getText();
  671.         format.applyPattern(timePattern);
  672.         millisFormat();
  673.         millisParse();
  674.     }
  675.  
  676.     private boolean add = false;
  677.  
  678.     /**
  679.      * This function is called when the "Roll" radio button is selected.
  680.      */
  681.     public  void clickedGetRoll() {
  682.         add=false;
  683.     }
  684.  
  685.     /**
  686.      * This function is called when the "Add" radio button is selected.
  687.      */
  688.     public void clickedGetAdd() {
  689.         add=true;
  690.     }
  691.  
  692.     /**
  693.      * This function is called when the "Date Format" radio button is selected.
  694.      */
  695.     public void clickedGetDateFormat() {
  696.         setFormatFromLocale(false);
  697.     }
  698.  
  699.     /**
  700.      * This function is called when the "Time Format" radio button is selected.
  701.      */
  702.     public void clickedGetTimeFormat() {
  703.         setFormatFromLocale(false);
  704.     }
  705.  
  706.     /**
  707.      * This function is called when the "Date and Time Format" radio button
  708.      * is selected.
  709.      */
  710.     public void clickedGetDateTimeFormat() {
  711.         setFormatFromLocale(false);
  712.     }
  713.  
  714.     /**
  715.      * This function is called when a new locale is selected.
  716.      */
  717.     public void selectedLocaleMenu() {
  718.         setFormatFromLocale(true);
  719.     }
  720.  
  721.     /**
  722.      * This function is called when a new Date (Format) Style is selected.
  723.      */
  724.     public void selectedDateStyleMenu() {
  725.         setFormatFromLocale(false);
  726.     }
  727.  
  728.     /**
  729.      * This function is called when a new Time (Format) Style is selected.
  730.      */
  731.     public void selectedTimeStyleMenu() {
  732.         setFormatFromLocale(false);
  733.     }
  734.  
  735.     /**
  736.      * Store the current time in milliseconds.
  737.      */
  738.     long time = System.currentTimeMillis();
  739.  
  740.     /**
  741.      * This function is called when it is necessary to parse the time
  742.      * string in the "1.0 Date" text field.
  743.      */
  744.     public void formatText() {
  745.         String leftString = inputText.getText();
  746.         if (leftString.length() == 0)
  747.         {
  748.             errorText("Error: no input to format!");
  749.             return;
  750.         }
  751.  
  752.         try {
  753.       // intentional use of deprecated method
  754.       time = Date.parse(leftString); 
  755.         }
  756.         catch (Exception e) {
  757.             outputText.setText("ERROR");
  758.             errorText("Exception: Date.parse: "+leftString);
  759.             return;
  760.         }
  761.         setMillisText();
  762.         millisFormat();
  763.     }
  764.  
  765.     /**
  766.      * This function is called when it is necessary to parse the time
  767.      * string in the "New Date" text field.
  768.      */
  769.     public void parseText() {
  770.         String rightString = outputText.getText();
  771.  
  772.         ParsePosition status = new ParsePosition(0);
  773.  
  774.         if (rightString.length() == 0)
  775.         {
  776.             errorText("Error: no input to parse!");
  777.             return;
  778.         }
  779.  
  780.         try {
  781.             time = format.parse(rightString, status).getTime();
  782.         }
  783.         catch (Exception e) {
  784.             inputText.setText("ERROR");
  785.             errorText("Exception: parse: "+rightString);
  786.             return;
  787.         }
  788.         setMillisText();
  789.         millisParse();
  790.  
  791.     }
  792.  
  793.     /**
  794.      * This function is called when it is necessary to format the time
  795.      * in the "Millis" text field.
  796.      */
  797.     public void millisFormat() {
  798.         String out = "";
  799.         try {
  800.             out = format.format(new Date(time));
  801.         }
  802.         catch (Exception e) {
  803.             outputText.setText("ERROR");
  804.             errorText("Exception: format: "+time);
  805.             return;
  806.         }
  807.         outputText.setText( out );
  808.         errorText("Formatted...");
  809.     }
  810.  
  811.     /**
  812.      * This function is called when it is necessary to display the time
  813.      * value parsed using GMT string in the "1.0 Date" text field.
  814.      */
  815.     public void millisParse() {
  816.         String input = "";
  817.         try {
  818.       // intentional use of deprecated method
  819.       input = new Date(time).toGMTString(); 
  820.             }
  821.         catch (Exception e) {
  822.             inputText.setText("ERROR");
  823.             errorText("Exception: in toGMTString: "+time); 
  824.             return;
  825.         }
  826.         inputText.setText( input );
  827.         errorText("Parsed...");
  828.     }
  829.  
  830.     /**
  831.      * This function is called when the time value in the "Millis" text field
  832.      * is changed. The new time value will be formatted and displayed in both
  833.      * "New Date" and "1.0 Date" text fields.
  834.      */
  835.     public void millisChanged() {
  836.         String millisString = millisText.getText();
  837.         try {
  838.             time = Long.parseLong(millisString);
  839.             }
  840.         catch (Exception e) {
  841.             errorText("Exception: Bad value for millis. Must be Long");
  842.             return;
  843.         }
  844.         millisFormat();
  845.         millisParse();
  846.         errorText("Millis changed...");
  847.     }
  848.  
  849.     /**
  850.      * This function is called when it is necessary to display the time
  851.      * value in milliseconds in the "Millis" text field.
  852.      */
  853.     public void setMillisText() {
  854.         millisText.setText(Long.toString(time));
  855.     }
  856.  
  857.     /**
  858.      * This function is called when users change the pattern text.
  859.      */
  860.     public void patternTextChanged() {
  861.         setFormatFromPattern();
  862.     }
  863.  
  864.     /**
  865.      * This function is called when users select a new representative city.
  866.      */
  867.     public void cityChanged() {
  868.         int index = cityMenu.getSelectedIndex();
  869.  
  870.         SimpleTimeZone timeZone = new SimpleTimeZone(kZoneOffsets[index],
  871.                                                      kZoneIDs[index]);
  872.         timeZone.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY,
  873.             2 * millisPerHour);
  874.         timeZone.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY,
  875.             2 * millisPerHour);
  876.  
  877.         format.setTimeZone(timeZone);
  878.  
  879.         millisFormat();
  880.         millisParse();
  881.     }
  882.  
  883.     private boolean addMode() {
  884.         return add;
  885.     }
  886.  
  887.     /**
  888.      * This function is called when users select a new time field
  889.      * to add or roll its value.
  890.      */
  891.     public void dateFieldChanged(boolean up) {
  892.         String d = dateMenu.getSelectedItem();
  893.         byte field = 0;
  894.  
  895.         if (d.equals("Year")) {
  896.             field = (byte) Calendar.YEAR;
  897.         } else
  898.         if (d.equals("Month")) {
  899.             field = (byte) Calendar.MONTH;
  900.         } else
  901.         if (d.equals("Day of Month")) {
  902.             field = (byte) Calendar.DATE;
  903.         } else
  904.         if (d.equals("Hour of Day")) {
  905.             field = (byte) Calendar.HOUR_OF_DAY;
  906.         } else
  907.         if (d.equals("Minute")) {
  908.             field = (byte) Calendar.MINUTE;
  909.         } else
  910.         if (d.equals("Second")) {
  911.             field = (byte) Calendar.SECOND;
  912.         } else
  913.         if (d.equals("Millisecond")) {
  914.             field = (byte) Calendar.MILLISECOND;
  915.         }
  916.  
  917.         format.getCalendar().setTime(new Date(time));
  918.     //        format.getCalendar().computeFields();
  919.  
  920.         if (up) {
  921.             if (addMode()) {
  922.                 format.getCalendar().add(field, 1);
  923.             } else {
  924.                 format.getCalendar().roll(field, true);
  925.             }
  926.         } else {
  927.             if (addMode()) {
  928.                 format.getCalendar().add(field, -1);
  929.             } else {
  930.                 format.getCalendar().roll(field, false);
  931.             }
  932.         }
  933.  
  934.         time = format.getCalendar().getTime().getTime();
  935.  
  936.         setMillisText();
  937.  
  938.         millisFormat();
  939.  
  940.         millisParse();
  941.  
  942.     }
  943.  
  944.     /**
  945.      * Print out the error message while debugging this program.
  946.      */
  947.     public void errorText(String s)
  948.     {
  949.         if (DEBUG)
  950.         {
  951.             System.out.println(s);
  952.         }
  953.     }
  954.  
  955. }
  956.  
  957.  
  958.